home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / weightpaint_invert.py < prev    next >
Text File  |  2009-08-31  |  3KB  |  95 lines

  1. #!BPY
  2. """
  3. Name: 'Invert Active Group'
  4. Blender: 245
  5. Group: 'WeightPaint'
  6. Tooltip: 'Invert the active vertex group'
  7. """
  8.  
  9. # -------------------------------------------------------------------------- 
  10. # ***** BEGIN GPL LICENSE BLOCK ***** 
  11. # This program is free software; you can redistribute it and/or 
  12. # modify it under the terms of the GNU General Public License 
  13. # as published by the Free Software Foundation; either version 2 
  14. # of the License, or (at your option) any later version. 
  15. # This program is distributed in the hope that it will be useful, 
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  18. # GNU General Public License for more details. 
  19. # You should have received a copy of the GNU General Public License 
  20. # along with this program; if not, write to the Free Software Foundation, 
  21. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
  22. # ***** END GPL LICENCE BLOCK ***** 
  23. # --------------------------------------------------------------------------
  24. import Blender
  25. from Blender import Scene, Mesh, Window, sys
  26.  
  27. import BPyMessages
  28. import bpy
  29.  
  30. def vgroup_invert(ob_orig, me):
  31.     if not me.getVertGroupNames():
  32.         return
  33.     group_act = me.activeGroup
  34.     if group_act == None:
  35.         return
  36.     
  37.     group_data = me.getVertsFromGroup(group_act, 1)
  38.     
  39.     weights= [1.0] * len(me.verts) # 1.0 - initialize inverted
  40.     
  41.     group_data = me.getVertsFromGroup(group_act, 1) # (i,w)  tuples.
  42.     
  43.     me.removeVertGroup(group_act) # messes up the active group.
  44.     for i,w in group_data:
  45.         weights[i] = 1.0-w
  46.     
  47.     me.addVertGroup(group_act)
  48.     
  49.     rep = Blender.Mesh.AssignModes.REPLACE
  50.     vertList= [None]
  51.     for i,weight in enumerate(weights):
  52.         vertList[0] = i
  53.         me.assignVertsToGroup(group_act, vertList, weight, rep)
  54.     
  55.     me.activeGroup = group_act
  56.     me.update()
  57.  
  58. def main():
  59.     
  60.     # Gets the current scene, there can be many scenes in 1 blend file.
  61.     sce = bpy.data.scenes.active
  62.     
  63.     # Get the active object, there can only ever be 1
  64.     # and the active object is always the editmode object.
  65.     ob_act = sce.objects.active
  66.     
  67.     if not ob_act or ob_act.type != 'Mesh':
  68.         BPyMessages.Error_NoMeshActive()
  69.         return 
  70.     
  71.     # Saves the editmode state and go's out of 
  72.     # editmode if its enabled, we cant make
  73.     # changes to the mesh data while in editmode.
  74.     is_editmode = Window.EditMode()
  75.     Window.EditMode(0)
  76.     
  77.     Window.WaitCursor(1)
  78.     me = ob_act.getData(mesh=1) # old NMesh api is default
  79.     t = sys.time()
  80.     
  81.     # Run the mesh editing function
  82.     vgroup_invert(ob_act, me)
  83.     
  84.     # Timing the script is a good way to be aware on any speed hits when scripting
  85.     print 'Invert VGroup in %.2f seconds' % (sys.time()-t)
  86.     Window.WaitCursor(0)
  87.     if is_editmode: Window.EditMode(1)
  88.     
  89. # This lets you can import the script without running it
  90. if __name__ == '__main__':
  91.     main()